﻿using MySql.Data.MySqlClient;
using NSimple.Data.MySql;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace hmc.im.service.core
{
    public class OrderNotify
    {
        /// <summary>
        /// 获取最新订单条数
        /// </summary>
        /// <param name="start"></param>
        /// <returns></returns>
        public int GetNewOrderCount(long start, out long end)
        {
            //string sql = "SELECT COUNT(0) FROM ze_orders WHERE create_time>unix_timestamp('" + start.ToString("yyyy-MM-dd HH:mm:ss") + "');SELECT FROM_UNIXTIME(create_time) FROM ze_orders ORDER BY create_time desc;";
            string sql = "SELECT COUNT(0) FROM ze_orders WHERE create_time>" + start + ";SELECT create_time FROM ze_orders ORDER BY create_time desc;";



            string connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            
            //if (DBUtils.Instance.Connection.State == System.Data.ConnectionState.Closed)
            //    DBUtils.Instance.Connection.Open();

            //MySqlCommand cmd = null;
            MySqlDataReader dr = null;
            int result = 0;
            end = 0;
            try
            {
                //cmd = new MySqlCommand(sql, DBUtils.Instance.Connection);

                //cmd.ExecuteReader();//
                dr = SqlHelper.ExecuteReader(connString, System.Data.CommandType.Text, sql);

                if (dr.Read())
                {
                    result = dr.GetInt32(0);
                }
                if (dr.NextResult())
                {
                    dr.Read();
                    end = dr.GetInt64(0);
                }
                else end = start;
                //object result = SqlHelper.ExecuteScalar(ConfigurationManager.AppSettings["ConnectionString"], System.Data.CommandType.Text, sql);

                //cmd.Dispose();
                //dr.Dispose();
                //dr.Close();
            }
            catch (Exception ex)
            {
                Logger.Writer(ex);
            }
            finally
            {
                //cmd.Dispose();
                dr.Dispose();
                dr.Close();

            }
            return result;
        }
    }
}
